<?php

namespace app\admin\controller;

use app\admin\model\AdminLog;
use app\admin\model\Gateway;
use app\admin\model\Admin;
use app\common\controller\Backend;
use think\Config;
use think\Db;
use think\Hook;
use think\Request;
use think\Validate;
use think\Env;

use app\common\library\FSHelper;

/**
 * 后台首页
 * @internal
 */
class Index extends Backend
{

    protected $noNeedLogin = ['login'];
    protected $noNeedRight = ['index', 'logout', 'home'];
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 后台首页
     */
    public function index()
    {
        //左侧菜单
        $menulist = $this->auth->getSidebar([
            'dashboard' => 'hot',
            'addon' => ['new', 'red', 'badge'],
            'auth/rule' => __('Menu'),
            'general' => ['new', 'purple'],
        ], $this->view->site['fixedpage']);
        $action = $this->request->request('action');
        if ($this->request->isPost()) {
            if ($action == 'refreshmenu') {
                $this->success('', null, ['menulist' => $menulist]);
            }
        }
        $gatewayList = collection(Gateway::field('name')->where('admin_id', '=', $this->auth->id)->select())->toArray();
        $gateways = array();
        foreach ($gatewayList as $value) {
            $gateways[] = $value['name'];
        }
        $user = Admin::field('extension, extension_password')->where('id', '=', $this->auth->id)->find();
        $extension = array(
            'extension' => $user['extension'],
            'extension_password' => $user['extension_password'],
            'gateways' => implode(' | ', $gateways),
            'ws_server' => Env::get('ws.server', ''),
            'sip_server' => Env::get('sip.server', ''),
            'sip_port' => Env::get('sip.port', 5060),
            'sip_domain' => Env::get('sip.domain', ''),
            'sip_register_expires' => Env::get('sip.register.expires', 60),
            'sip_trace' => Env::get('sip.trace', false)
        );
        $extension['sip_domain'] = empty($extension['sip_domain']) ? $extension['sip_server'] : $extension['sip_domain'];
        $extension['sip_trace'] = $extension['sip_trace'] ? 'true' : 'false';
        $this->view->assign('extension', $extension);
        $this->view->assign('menulist', $menulist);
        $this->view->assign('title', __('Home'));
        return $this->view->fetch();
    }

    /**
     * 管理员登录
     */
    public function login()
    {
        $url = $this->request->get('url', 'index/index');
        if ($this->auth->isLogin()) {
            $this->success(__("You've logged in, do not login again"), $url);
        }
        if ($this->request->isPost()) {
            $username = $this->request->post('username');
            $password = $this->request->post('password');
            $keeplogin = $this->request->post('keeplogin');
            $token = $this->request->post('__token__');
            $rule = [
                'username' => 'require|length:3,30',
                'password' => 'require|length:3,30',
                '__token__' => 'token',
            ];
            $data = [
                'username' => $username,
                'password' => $password,
                '__token__' => $token,
            ];
            if (Config::get('fastadmin.login_captcha')) {
                $rule['captcha'] = 'require|captcha';
                $data['captcha'] = $this->request->post('captcha');
            }
            $validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
            $result = $validate->check($data);
            if (!$result) {
                $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
            }
            AdminLog::setTitle(__('Login'));
            $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
            if ($result === true) {
                Hook::listen("admin_login_after", $this->request);
                $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
            } else {
                $msg = $this->auth->getError();
                $msg = $msg ? $msg : __('Username or password is incorrect');
                $this->error($msg, $url, ['token' => $this->request->token()]);
            }
        }

        // 根据客户端的cookie,判断是否可以自动登录
        if ($this->auth->autologin()) {
            $this->redirect($url);
        }
        $background = Config::get('fastadmin.login_background');
        $background = stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background;
        $this->view->assign('background', $background);
        $this->view->assign('title', __('Login'));
        Hook::listen("admin_login_init", $this->request);
        return $this->view->fetch();
    }

    /**
     * 注销登录
     */
    public function logout()
    {
        $this->auth->logout();
        Hook::listen("admin_logout_after", $this->request);
        $this->redirect('index/login');
//        $this->success(__('Logout successful'), 'index/login');

    }

    public function setAgentStatus()
    {
        if (!$this->request->isPost()) {
            $this->error(__('You have no permission'));
        }
        $status = $this->request->request('status');
        if (!in_array($status, array('busy', 'idle'))) {
            $this->error(__('Invalid parameters'));
        }
        $model = Admin::field('extension')->where('id', '=', $this->auth->id)->find();
        if (empty($model) || empty($model->extension)) {
            $this->error(__('该用户无分机号'));
        }
        $fsHelper = new FSHelper();
        $currentStatus = $fsHelper->getAgentStatus($model->extension);
        if ($status == $currentStatus) {
            $fsHelper->disconnect();
            $this->success();
        }
        $result = $fsHelper->setAgentStatus($model->extension, $status);
        $fsHelper->disconnect();
        if (!$result) {
            $this->error(__('设置分机忙闲状态失败！'));
        }
        $this->success();
    }

    public function getAgentStatus()
    {
        if (!$this->request->isPost()) {
            $this->error(__('You have no permission'));
        }
        $model = Admin::field('extension')->where('id', '=', $this->auth->id)->find();
        if (empty($model) || empty($model->extension)) {
            $this->error(__('该用户无分机号'));
        }
        $fsHelper = new FSHelper();
        $currentStatus = $fsHelper->getAgentStatus($model->extension);
        $fsHelper->disconnect();
        if ($currentStatus === false) {
            $this->error(__('获取分机忙闲状态失败！'));
        }
        $this->success('', null, array('status' => $currentStatus));
    }


    /**
     * @param Request $request
     * @return \think\response\Json
     * 后台首页
     */
    public function home(Request $request)
    {

        if (!$this->auth->id) {
            return json(['code' => 0, 'data' => [], 'msg' => '非法请求，请登录']);
        }

        $request = Request::instance();
        $today_start=strtotime(date('Y-m-d 00:00:00'));
        $today_end=strtotime(date('Y-m-d 23:59:59'));

        /************线路数量*************/
        $lineInfo = Db::name('task')
            ->where('admin_id', $this->auth->id)
            ->select();
        //正在执行中的任务数量
        $runningLine = 0;
        foreach ($lineInfo as $value) {
            if ($value['state'] == '1') {
                $runningLine += 1;
            }
        }
        $lineNUm = Count($lineInfo);

        //线路数据量

        if ($request->get('start_time_line') && $request->get('end_time_line')) {
            $end_time_line = strtotime(date('Y-m-d ', $request->get('end_time_line')) . "23:59:59");
            $timeLine = [$request->get('start_time_line') ?: 0, $end_time_line];
        }else{
            $timeLine = [$today_start, $today_end];
        }
        $dataLine = $this->census($timeLine, $this->auth->id);

        /****************环状图数据统计***************/


        if ($request->get('start_time_pic2') && $request->get('end_time_pic2')) {
            $end_time_lpic2 = strtotime(date('Y-m-d ', $request->get('end_time_pic2')) . "23:59:59");
            $timeLine = [$request->get('start_time_pic2') ?: 0, $end_time_lpic2];
        }else{
            $timeLine=[$today_start,$today_end];
        }
        $numInfo = $this->census($timeLine, $this->auth->id, 2);


        $data = [
            'line_num' => $lineNUm,//工作的总线路
            'running_line' => $runningLine,//正在执行的线路
            'user_num' => $dataLine['saler'],//用户总数
            'total_calls' => $dataLine['total_calls'],//呼叫总数
            'success_calls' => $dataLine['success_calls'],//成功呼叫总数
            'fail_calls' => $dataLine['fail_calls'],//失败呼叫总数
            'A_calls' => $dataLine['A_calls'],//A类客户
            'B_calls' => $dataLine['B_calls'],//B类客户
            'pic_time' => [],//折线图的时间节点
            'pic_call' => [],//折线图的拨打总数
            'pic_success' => [],//折线图的拨通成功总数
            'A_num' => $numInfo['A_num'],
            'B_num' => $numInfo['B_num'],
            'C_num' => $numInfo['C_num'],
        ];

        ###############################折线图##############################

        // 时间统计
        if ($request->get('start_time_pic') && $request->get('end_time_pic')) {
            $request->get('end_time_pic') ?strtotime(date('Y-m-d ', $request->get('end_time_pic')) . "23:59:59"): time();
            $request->get('start_time_pic') ?: 0;
            $time = $request->get('end_time_pic') - $request->get('start_time_pic');

            if ($time < 86400 && $time >= 0) {

                for ($h = 0; $h <= 23; $h++) {
                    $times = [
                        strtotime(date('Y-m-d' . $h . ':00:00', $request->get('start_time_pic'))),
                        strtotime(date('Y-m-d' . $h . ':59:59', $request->get('start_time_pic'))),
                    ];
                    $this_data = $this->census($times, $this->auth->id, 1);

                    $data['pic_time'][] = $h . '点';
                    $data['pic_call'][] = $this_data ? $this_data['connect_success'] : 0;
                    $data['pic_success'][] = $this_data ? $this_data['connect_total'] : 0;
                }
            } elseif ($time >= 86400 && $time <= 86400 * 30) {
                $day = intval($time / 86400) + 1;//时间段内隔了多少天
                while ($day > 0) {
                    $date = date('Y-m-d', $request->get('end_time_pic'));
                    $times = [
                        strtotime($date . ' 00:00:00') - 86400 * $day,
                        strtotime($date . ' 23:59:59') - 86400 * $day,
                    ];
                    $this_data = $this->census($times, $this->auth->id, 1);

                    $data['pic_time'][] = date('Y-m-d', strtotime('-' . $day . 'days'));
                    $data['pic_success'][] = $this_data ? $this_data['connect_success'] : 0;
                    $data['pic_call'][] = $this_data ? $this_data['connect_total'] : 0;
                    $day--;
                }

            } elseif ($time >= 86400 * 30 && $time < 86400 * 365) {
                $month = intval($time / (86400 * 30));//中间隔了多少个月份

                while ($month >= 0) {
                    $date = date('Y-m', $request->get('end_time_pic'));//参数的月份

                    $times = [
                        strtotime($date . '-1 00:00:00' . ' -' . $month . 'month'),//循环减掉$month个月的1号的日期
                        strtotime($date . '-' . date('t', $request->get('end_time_pic')) . ' 23:59:59' . ' -' . $month . 'month')
                    ];
                    $this_data = $this->census($times, $this->auth->id, 1);

                    $data['pic_time'][] = date('Y-m', strtotime('-' . $month . 'month'));
                    $data['pic_success'][] = $this_data ? $this_data['connect_success'] : 0;
                    $data['pic_call'][] = $this_data ? $this_data['connect_total'] : 0;
                    $month--;

                }

            } elseif ($time >= 86400 * 365) {
                $year = intval($time / (86400 * 365));
                while ($year >= 0) {
                    $date = date('Y', $request->get('end_time_pic'));
                    $times = [
                        strtotime($date . '1-1 00:00:00') - 86400 * 365 * $year,
                        strtotime($date . '12-31 23:59:59') - 86400 * 365 * $year,
                    ];

                    $this_data = $this->census($times, $this->auth->id, 1);

                    $data['pic_time'][] = date('Y', strtotime('-' . $year . 'year'));
                    $data['pic_success'][] = $this_data ? $this_data['connect_success'] : 0;
                    $data['pic_call'][] = $this_data ? $this_data['connect_total'] : 0;
                    $year--;
                }

            }
        }

        return json($data);
    }


    /**
     * @param $date
     * @param $userId
     * @param int $type
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 数据统计
     */
    public function census($date, $userId, $type = 0)
    {
        if (!$type) {
            $dataSaler = Db::name('saler')
                ->where('admin_id', $userId)
                ->count();
            $dataResult = Db::name('task_job')
                ->where('admin_id', $userId)
                ->where('calltime', 'between', $date)
                ->field('callstate,grade')
                ->select();

//            P(DB::table('task_job')->getLastSql());
            $data_call_count = $data_call_success = $data_call_fail = $data_call_A = $data_call_B = 0;

            foreach ($dataResult as $value) {
                if ($value['callstate'] == 'success') {
                    $data_call_success += 1;
                }
                if ($value['callstate'] == 'fail') {
                    $data_call_fail += 1;
                }
                if ($value['grade'] == 'A') {
                    $data_call_A += 1;
                }
                if ($value['grade'] == 'B') {
                    $data_call_B += 1;
                }
                $data_call_count += 1;
            }

            $response = [
                'saler' => $dataSaler,
                'total_calls' => $data_call_count,
                'success_calls' => $data_call_success,
                'fail_calls' => $data_call_fail,
                'A_calls' => $data_call_A,
                'B_calls' => $data_call_B,
            ];
            return $response;

        }

        if ($type == 1) {
            $dataInfo = Db::name('task_job')
                ->where('admin_id', $userId)
                ->where('calltime', 'between', $date)
                ->field('callstate,grade,faqnum')
                ->select();

            $connect_success = $connect_total = 0;


            foreach ($dataInfo as $value) {
                if ($value['callstate'] == 'success') {
                    $connect_success += 1;
                }
                $connect_total += 1;

            }
            $response = [
                'connect_success' => $connect_success,
                'connect_total' => $connect_total,
            ];
            return $response;
        }


        if ($type == 2) {
            $result = Db::name('task_job')
                ->where('admin_id', $userId)
                ->where('calltime', 'between', $date)
                ->field('callstate,grade')
                ->select();

            $data_call_A = $data_call_B = $data_call_C = 0;

            foreach ($result as $value) {
                if ($value['grade'] == 'A') {
                    $data_call_A += 1;
                }

                if ($value['grade'] == 'B') {
                    $data_call_B += 1;
                }

                if ($value['grade'] == 'C') {
                    $data_call_C += 1;
                }

            }

            $response = [
                'A_num' => $data_call_A,
                'B_num' => $data_call_B,
                'C_num' => $data_call_C,
            ];

            return $response;
        }

    }





    /**
     * @param Request $request
     * @return \think\response\Json
     * 后台首页
     */
//    public function home(Request $request)
//    {
//
//        if (!$this->auth->id) {
//            return json(['code' => 0, 'data' => [], 'msg' => '非法请求，请登录']);
//        }
//
//        $request = Request::instance();
//
//        $dt = $request->get('dt');
//        $dt_line = $request->get('dt_line');
//        $dt_pic2 = $request->get('dt_pic2');
//
//        $today = [strtotime(date('Y-m-d 00:00:00')), strtotime(date('Y-m-d 23:59:59'))];
//        $lastToday = [strtotime(date('Y-m-d 00:00:00', strtotime('-1 day'))), strtotime(date('Y-m-d 23:59:59', strtotime('-1 day')))];
//        $sevenToday = [strtotime(date('Y-m-d 00:00:00', strtotime('-7 days'))), strtotime(date('Y-m-d 23:59:59'))];
//        $thirtyToday = [strtotime(date('Y-m-d 00:00:00', strtotime('-30 days'))), strtotime(date('Y-m-d 23:59:59'))];
//
//
//        /************线路数量*************/
//        $lineInfo = Db::name('task')
//            ->where('admin_id', $this->auth->id)
//            ->select();
//        //正在执行中的任务数量
//        $runningLine = 0;
//        foreach ($lineInfo as $value) {
//            if ($value['state'] == '1') {
//                $runningLine += 1;
//            }
//        }
//        $lineNUm = Count($lineInfo);
//
//        //线路数据量
//
//        if (!$request->get('start_time_line') && !$request->get('end_time_line')) {
//            if ($dt_line == 'last_today_line') {
//                $dataLine = $this->census($lastToday, $this->auth->id);
//            } elseif ($dt_line == 'seven_day_line') {
//                $dataLine = $this->census($sevenToday, $this->auth->id);
//            } elseif ($dt_line == 'thirty_day_line') {
//                $dataLine = $this->census($thirtyToday, $this->auth->id);
//            } elseif ($dt_line == 'today_line') {
//                $dataLine = $this->census($today, $this->auth->id);
//            }
//        }
//
//        if ($request->get('start_time_line') || $request->get('end_time_line')) {
//            $end_time_line = $request->get('end_time_line') ?: time();
//            $end_time_line = strtotime(date('Y-m-d ', $end_time_line) . "23:59:59");
//            $timeLine = [$request->get('start_time_line') ?: 0, $end_time_line];
//            $dataLine = $this->census($timeLine, $this->auth->id);
//        }
//
//        /****************环状图数据统计***************/
//
//
//        if ($dt_pic2 == 'last_today_pic2') {
//            $numInfo = $this->census($lastToday, $this->auth->id, 2);
//        } elseif ($dt_pic2 == 'seven_day_pic2') {
//            $numInfo = $this->census($sevenToday, $this->auth->id, 2);
//        } elseif ($dt_pic2 == 'thirty_day_pic2') {
//            $numInfo = $this->census($thirtyToday, $this->auth->id, 2);
//        } elseif ($dt_pic2 == 'today_pic2') {
//            $numInfo = $this->census($today, $this->auth->id, 2);
//        } elseif ($request->get('start_time_pic2') || $request->get('end_time_pic2')) {
//            $end_time_lpic2 = $request->get('end_time_lpic2') ?: time();
//            $end_time_lpic2 = strtotime(date('Y-m-d ', $end_time_lpic2) . "23:59:59");
//            $timeLine = [$request->get('start_time_pic2') ?: 0, $end_time_lpic2];
//            $numInfo = $this->census($timeLine, $this->auth->id, 2);
//        }
//
//
//        $data = [
//            'line_num' => $lineNUm,//工作的总线路
//            'running_line' => $runningLine,//正在执行的线路
//            'user_num' => $dataLine['saler'],//用户总数
//            'total_calls' => $dataLine['total_calls'],//呼叫总数
//            'success_calls' => $dataLine['success_calls'],//成功呼叫总数
//            'fail_calls' => $dataLine['fail_calls'],//失败呼叫总数
//            'A_calls' => $dataLine['A_calls'],//A类客户
//            'B_calls' => $dataLine['B_calls'],//B类客户
//            'pic_time' => [],//折线图的时间节点
//            'pic_call' => [],//折线图的拨打总数
//            'pic_success' => [],//折线图的拨通成功总数
//            'A_num' => $numInfo['A_num'],
//            'B_num' => $numInfo['B_num'],
//            'C_num' => $numInfo['C_num'],
//        ];
//
//        ###############################折线图##############################
//
//        // 今日数据统计
//        if ($dt == 'today_pic' && (!$request->get('start_time_pic') || !$request->get('end_time_pic'))) {
//            for ($h = 0; $h <= date('H'); $h++) {
//                $times = [
//                    strtotime(date('Y-m-d ' . $h . ':00:00')),
//                    strtotime(date('Y-m-d ' . $h . ':59:59')),
//                ];
//                $this_data = $this->census($times, $this->auth->id, 1);
//                $data['pic_time'][] = $h . '点';
//                $data['pic_call'][] = $this_data ? $this_data['connect_success'] : 0;
//                $data['pic_success'][] = $this_data ? $this_data['connect_total'] : 0;
//            }
//        } else if ($request->get('start_time_pic') or $request->get('end_time_pic')) {
//            $request->get('end_time_pic') ?: time();
//            $request->get('start_time_pic') ?: 0;
//            $time = $request->get('end_time_pic') - $request->get('start_time_pic');
//
//            if ($time < 86400 && $time >= 0) {
//
//                for ($h = 0; $h <= 23; $h++) {
//                    $times = [
//                        strtotime(date('Y-m-d' . $h . ':00:00', $request->get('start_time_pic'))),
//                        strtotime(date('Y-m-d' . $h . ':59:59', $request->get('start_time_pic'))),
//                    ];
//                    $this_data = $this->census($times, $this->auth->id, 1);
//
//                    $data['pic_time'][] = $h . '点';
//                    $data['pic_call'][] = $this_data ? $this_data['connect_success'] : 0;
//                    $data['pic_success'][] = $this_data ? $this_data['connect_total'] : 0;
//                }
//            } elseif ($time >= 86400 && $time <= 86400 * 30) {
//                $day = intval($time / 86400) + 1;//时间段内隔了多少天
//                while ($day > 0) {
//                    $date = date('Y-m-d', $request->get('end_time_pic'));
//                    $times = [
//                        strtotime($date . ' 00:00:00') - 86400 * $day,
//                        strtotime($date . ' 23:59:59') - 86400 * $day,
//                    ];
//                    $this_data = $this->census($times, $this->auth->id, 1);
//
//                    $data['pic_time'][] = date('Y-m-d', strtotime('-' . $day . 'days'));
//                    $data['pic_success'][] = $this_data ? $this_data['connect_success'] : 0;
//                    $data['pic_call'][] = $this_data ? $this_data['connect_total'] : 0;
//                    $day--;
//                }
//
//            } elseif ($time >= 86400 * 30 && $time < 86400 * 365) {
//                $month = intval($time / (86400 * 30));//中间隔了多少个月份
//
//                while ($month >= 0) {
//                    $date = date('Y-m', $request->get('end_time_pic'));//参数的月份
//
//                    $times = [
//                        strtotime($date . '-1 00:00:00' . ' -' . $month . 'month'),//循环减掉$month个月的1号的日期
//                        strtotime($date . '-' . date('t', $request->get('end_time_pic')) . ' 23:59:59' . ' -' . $month . 'month')
//                    ];
//                    $this_data = $this->census($times, $this->auth->id, 1);
//
//                    $data['pic_time'][] = date('Y-m', strtotime('-' . $month . 'month'));
//                    $data['pic_success'][] = $this_data ? $this_data['connect_success'] : 0;
//                    $data['pic_call'][] = $this_data ? $this_data['connect_total'] : 0;
//                    $month--;
//
//                }
//
//            } elseif ($time >= 86400 * 365) {
//                $year = intval($time / (86400 * 365));
//                while ($year >= 0) {
//                    $date = date('Y', $request->get('end_time_pic'));
//                    $times = [
//                        strtotime($date . '1-1 00:00:00') - 86400 * 365 * $year,
//                        strtotime($date . '12-31 23:59:59') - 86400 * 365 * $year,
//                    ];
//
//                    $this_data = $this->census($times, $this->auth->id, 1);
//
//                    $data['pic_time'][] = date('Y', strtotime('-' . $year . 'year'));
//                    $data['pic_success'][] = $this_data ? $this_data['connect_success'] : 0;
//                    $data['pic_call'][] = $this_data ? $this_data['connect_total'] : 0;
//                    $year--;
//                }
//
//            }
//
//        }
//
//        if (!$request->get('start_time_pic') || !$request->get('end_time_pic')) {
//
//            //昨日数据统计
//            if ($dt == 'last_today_pic') {
//                for ($h = 0; $h <= 24; $h++) {
//                    $times = [
//                        strtotime(date('Y-m-d ' . $h . ':00:00'), strtotime('-1 day')),
//                        strtotime(date('Y-m-d ' . $h . ':59:59'), strtotime('-1 day')),
//                    ];
//                    $this_data = $this->census($times, $this->auth->id, 1);
//                    $data['pic_time'][] = $h . '点';
//                    $data['pic_success'][] = $this_data ? $this_data['connect_success'] : 0;
//                    $data['pic_call'][] = $this_data ? $this_data['connect_total'] : 0;
//                }
//            }
//
//            //7天数据统计
//            if ($dt == 'seven_day_pic') {
//
//                $day = 7;
//                while ($day > 0) {
//                    $date = date('Y-m-d');
//                    $times = [
//                        strtotime($date . ' 00:00:00') - 86400 * $day,
//                        strtotime($date . ' 23:59:59') - 86400 * $day,
//                    ];
//                    $this_data = $this->census($times, $this->auth->id, 1);
//
//                    $data['pic_time'][] = date('Y-m-d', strtotime('-' . $day . 'days'));
//                    $data['pic_success'][] = $this_data ? $this_data['connect_success'] : 0;
//                    $data['pic_call'][] = $this_data ? $this_data['connect_total'] : 0;
//                    $day--;
//                }
//            }
//
//            //30天数据统计
//            if ($dt == 'thirty_day_pic') {
//
//                $day = 30;
//                while ($day > 0) {
//                    $date = date('Y-m-d');
//                    $times = [
//                        strtotime($date . ' 00:00:00') - 86400 * $day,
//                        strtotime($date . ' 23:59:59') - 86400 * $day,
//                    ];
//                    $this_data = $this->census($times, $this->auth->id, 1);
//
//                    $data['pic_time'][] = date('Y-m-d', strtotime('-' . $day . 'days'));
//                    $data['pic_success'][] = $this_data ? $this_data['connect_success'] : 0;
//                    $data['pic_call'][] = $this_data ? $this_data['connect_total'] : 0;
//                    $day--;
//                }
//            }
//        }
//        return json($data);
//    }





}
